﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace SFJDManagement
{
    /// <summary>
    /// 日期选择复合按钮
    /// UC_MultiDateChoice.xaml 的交互逻辑
    /// </summary>
    public partial class UC_MultiDateChoice : UserControl
    {
        public StackPanel layout = new StackPanel();
        public WrapPanel radioPanel = new WrapPanel();
        private List<string> listName = new List<string>(5);
        private DataGrid dataGrid;
        public delegate void TimeChangedHandler(object sender, EventArgs e);
        public event TimeChangedHandler TimeChanged;
        public DateTime timeStart = DateTime.MinValue;
        public DateTime timeEnd = DateTime.MaxValue;
        public string choiced = "";

        public UC_MultiDateChoice(DataGrid dataGrid)
        {
            this.dataGrid = dataGrid;
            GetName();
            InitializeComponent();
            Ini();
            Content = layout;
        }

        private void Ini()
        {
            layout.Orientation = Orientation.Horizontal;
            layout.Background = Brushes.Azure;
            foreach (string str in listName)
            {
                radioPanel.Children.Add(GetRadioButton(str));
            }
            radioPanel.Height = 50;
            radioPanel.Orientation = Orientation.Vertical;
            //(radioPanel.Children[0] as RadioButton).IsChecked = true;
            //choiced = (radioPanel.Children[0] as RadioButton).Content.ToString();
            layout.Children.Add(radioPanel);
            DatePicker datePickerStart = new DatePicker()
            {
                Margin = new Thickness(5),
                VerticalContentAlignment = VerticalAlignment.Center,
                Height = 30,
                MinWidth = 100
            };
            datePickerStart.Loaded += DatePickerStart_Loaded;
            datePickerStart.SelectedDateChanged += DatePickerStart_SelectedDateChanged;
            layout.Children.Add(datePickerStart);
            DatePicker datePickerEnd = new DatePicker()
            {
                Margin = new Thickness(5),
                VerticalContentAlignment = VerticalAlignment.Center,
                Height = 30,
                MinWidth = 100
            };
            datePickerEnd.Loaded += DatePickerEnd_Loaded;
            datePickerEnd.SelectedDateChanged += DatePickerEnd_SelectedDateChanged;
            layout.Children.Add(datePickerEnd);
        }

        private void DatePickerEnd_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            timeEnd = DateTime.Parse((sender as DatePicker).Text);
            TimeChanged(sender, e);
        }

        private void DatePickerStart_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            timeStart = DateTime.Parse((sender as DatePicker).Text);
            TimeChanged(sender, e);
        }

        private void GetName()
        {
            foreach(DataGridColumn column in dataGrid.Columns)
            {
                if(Regex.IsMatch(column.Header.ToString(), "时间$"))
                {
                    listName.Add(column.Header.ToString());
                }
            }
        }

        private RadioButton GetRadioButton(string name)
        {
            RadioButton b = new RadioButton();
            b.Name = name;
            b.Content = name;
            b.Margin = new Thickness(3);
            b.Checked += B_Checked;
            return b;
        }

        private void B_Checked(object sender, RoutedEventArgs e)
        {
            choiced = (sender as RadioButton).Content.ToString();
            TimeChanged(sender, e);
        }

        private void DatePickerEnd_Loaded(object sender, RoutedEventArgs e)
        {
            var dp = sender as DatePicker;
            if (dp == null) return;

            var tb = GetChildOfType<DatePickerTextBox>(dp);
            if (tb == null) return;

            var wm = tb.Template.FindName("PART_Watermark", tb) as ContentControl;
            if (wm == null) return;

            wm.Content = "终止日期";
        }

        private void DatePickerStart_Loaded(object sender, RoutedEventArgs e)
        {
            var dp = sender as DatePicker;
            if (dp == null) return;

            var tb = GetChildOfType<DatePickerTextBox>(dp);
            if (tb == null) return;

            var wm = tb.Template.FindName("PART_Watermark", tb) as ContentControl;
            if (wm == null) return;

            wm.Content = "起始日期";
        }

        public T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj == null) return null;

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                var child = VisualTreeHelper.GetChild(depObj, i);

                var result = (child as T) ?? GetChildOfType<T>(child);
                if (result != null) return result;
            }
            return null;
        }
    }
}
